<script setup>
import { ElMessage } from 'element-plus'

import { infoApi, createApi, updateApi } from '@/api/job'

const emits = defineEmits(['refresh'])

const refForm = ref()
const visible = ref(false)
const loading = ref(false)
const form = reactive({
  id: '',
  name: '',
  bean: '',
  params: '',
  cron: '',
  remark: '',
  status: 0
})
const rules = reactive({
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  bean: [{ required: true, message: '请输入处理器名称', trigger: 'blur' }],
  cron: [{ required: true, message: '请输入cron表达式', trigger: 'blur' }]
})

const init = async (id) => {
  visible.value = true
  loading.value = true
  form.id = id
  if (id) {
    const r = await infoApi({ id })
    if (r) {
      const { name, bean, params, cron, remark, status } = r.data
      form.name = name
      form.bean = bean
      form.params = params
      form.cron = cron
      form.remark = remark
      form.status = status
    }
  }
  nextTick(() => { loading.value = false })
}

/**
 * @description: 表单验证提交
 * @param {*}
 * @return {*}
 * @author: gumingchen
 */
const submit = () => {
  refForm.value.validate(async valid => {
    if (valid) {
      const r = form.id ? await updateApi(form) : await createApi(form)
      if (r) {
        visible.value = false
        ElMessage({
          message: '操作成功!',
          type: 'success'
        })
        emits('refresh')
      }
    }
  })
}

/**
 * @description: 弹窗关闭动画结束时的回调
 * @param {*}
 * @return {*}
 * @author: gumingchen
 */
const dialogClosedHandle = () => {
  refForm.value.resetFields()
}

defineExpose({
  init
})
</script>

<template>
  <el-dialog
    width="500px"
    :title="form.id ? '编辑' : '新增'"
    v-model="visible"
    :close-on-click-modal="false"
    @closed="dialogClosedHandle"
    append-to-body
    draggable>
    <el-form
      v-loading="loading"
      :model="form"
      :rules="rules"
      ref="refForm"
      label-width="110px"
      @keyup.enter="submit()">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="form.name"
          placeholder="名称"
          maxlength="50"
          show-word-limit />
      </el-form-item>
      <el-form-item label="处理器名称" prop="bean">
        <el-input
          v-model="form.bean"
          placeholder="处理器名称"
          maxlength="200" />
      </el-form-item>
      <el-form-item label="cron表达式" prop="cron">
        <el-input
          v-model="form.cron"
          placeholder="cron表达式"
          maxlength="100" />
      </el-form-item>
      <el-form-item label="参数" prop="params">
        <el-input
          v-model="form.params"
          placeholder="参数"
          type="textarea"
          maxlength="500"
          show-word-limit />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <DictRadio v-model="form.status" code="STATUS" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          placeholder="备注"
          type="textarea"
          maxlength="200"
          show-word-limit />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button
          v-repeat
          type="primary"
          @click="submit()">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>
